package com.spyrx7.fang_erhuo.util;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by pc-spyrx7 on 2015/12/31.
 * 作者  pc-spyrx7
 * 邮箱 spyhanfeng@qq.com
 */
public class VolleyUtil  {
    private volatile static RequestQueue requestQueue;

    public static RequestQueue getRequestQueue(Context context){
        if (requestQueue == null){
            synchronized (VolleyUtil.class){
                requestQueue = Volley.newRequestQueue(context.getApplicationContext());
            }
        }
        return requestQueue;
    }

    private static JSONObject object;

    private static int errcode = -1;

    public static boolean isErrcode(String s){
        object = stringToJson(s);
        if(object != null){
            try {
                if(object.getInt("errcode") == 0)  return  true;
            }catch (JSONException e){
                return false;
            }
        }

        return false;
    }

    public static String getMsg(String s){
        object = stringToJson(s);
        if(object != null){
            try {
                return  object.getString("msg");
            }catch (JSONException e){
                return null;
            }
        }

        return null;
    }

    public static String getToken(String s){
        object = stringToJson(s);
        if(object != null){
            try {
                return  object.getString("token");
            }catch (JSONException e){
                return null;
            }
        }

        return null;
    }

    public static JSONObject getObject(String s){

     try {
         s = object.getString("data");
     }catch (Exception e){
      }


        if(object != null){
            try {
                return  new JSONObject(s);
            }catch (Exception e){
                return null;
            }
        }
        return null;
    }


    public static JSONArray getArray(String s){
        try {
            s = object.getString("data");
            if(UIHelp.DEBUG) Log.e("toto",s);
        }catch (Exception e){
        }

        if(object != null){
            try {
                return  new JSONArray(s);
            }catch (JSONException e){
                return null;
            }
        }

        return null;
    }

    public static JSONObject stringToJson(String s){
        try {
            JSONObject jsonObject = new JSONObject(s);
            return  jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
